package org.muninn.niagara.services;

import javax.baja.collection.BIList;
import javax.baja.history.ext.BHistoryExt;
import javax.baja.naming.BOrd;
import javax.baja.sys.Action;
import javax.baja.sys.BComplex;
import javax.baja.sys.BComponent;
import javax.baja.sys.BIcon;
import javax.baja.sys.BRelTime;
import javax.baja.sys.Flags;
import javax.baja.sys.Property;
import javax.baja.sys.Sys;
import javax.baja.sys.Type;

import org.muninn.niagara.extensions.BCloudExt;

public class BCloudUploadService extends BMuninnService
{
  public static final Property numberOfExts = newProperty(Flags.READONLY, 0);
  public void setNumberOfExts(int v) { setInt(numberOfExts, v); }
  public int getNumberOfExts() { return getInt(numberOfExts); }

  public static final Property numberOfActiveExts = newProperty(Flags.READONLY, 0);
  public void setNumberOfActiveExts(int v) { setInt(numberOfActiveExts, v); }
  public int getNumberOfActiveExts() { return getInt(numberOfActiveExts); }

  public static final Property checkPeriod = newProperty(0, BRelTime.make(60000));
  public void setCheckPeriod(BRelTime t) { set(checkPeriod, t); }
  public BRelTime getCheckPeriod() { return (BRelTime) get(checkPeriod); }

  public static final Action refreshLists = newAction(0, null, null);
  public void refreshLists() { invoke(refreshLists, null); }
  public void doRefreshLists() { getLists(); }

  private static BCloudUploadService INSTANCE = null;
  private static BCloudExt[] cloudListAll;
  private static BCloudExt[] cloudListEn;

  public void started()
  {
    super.started();
    getLists();
  }

  public void getLists()
  {
    cloudListAll = findExts();
    cloudListEn = removeDisabledExts(cloudListAll);
    
    if (cloudListEn.length > 0)
    {
      BHistoryExt[] histList = getTaggedExts();
      BComponent[] pointList = getTaggedPoints();
    }
    
    setNumberOfExts(cloudListAll.length);
    setNumberOfActiveExts(cloudListEn.length);
  }
  
  private BComponent[] getTaggedPoints()
  {
    BHistoryExt[] list = getTaggedExts();
    if (list == null || list.length <= 0)
      return null;
  
    BComponent[] pointList = new BComponent[list.length];

    for (int i = 0; i < list.length; i++)
      pointList[i] = list[i].getParentComponent();

    return pointList;
  }

  private BHistoryExt[] getTaggedExts()
  {
    BCloudExt[] list = removeDisabledExts(findExts());
    if (list == null || list.length <= 0)
      return null;
    
    BHistoryExt[] pointList = new BHistoryExt[list.length];

    for (int i = 0; i < list.length; i++)
      pointList[i] = (BHistoryExt) (list[i].getParent());

    return pointList;
  }

  private BCloudExt[] removeDisabledExts(BCloudExt[] list)
  {
    BCloudExt[] trimmedList = new BCloudExt[list.length];

    for (int i = 0, j = 0; i < list.length; i++)
    {
      if (((BHistoryExt) (list[i].getParent())).getEnabled())
      {
        trimmedList[j] = list[i];
        j++;
      }
    }

    return trimmedList;
  }

  private BCloudExt[] findExts()
  {
    BIList list = (BIList) BOrd.make("station:|slot:/Drivers|bql:select * from muninn:CloudExt where enable=true").resolve().get();
    BCloudExt exts[] = new BCloudExt[list.size()];
    exts = (BCloudExt[]) list.list(exts);
    System.out.println("arraySize: " + exts.length);

    return exts;
  }

  public BIcon getIcon() { return icon; }
  private static final BIcon icon = BIcon.make("local:|module://muninn/org/muninn/niagara/graphics/MuninnGroup.PNG");

  public static final Type TYPE = Sys.loadType(BCloudUploadService.class);
  public Type[] getServiceTypes() { return new Type[] { TYPE }; }
  public Type getType() { return TYPE; }

  public final synchronized void serviceStarted() throws Exception { INSTANCE = this; }

  public final synchronized void serviceStopped() throws Exception { INSTANCE = null; }

  public static final BCloudUploadService getInstance() { return INSTANCE; }
}
